package com.jstyle.beatecg.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.jstyle.beatecg.R;
import com.jstyle.beatecg.activity.EcgWatchActivity;
import com.jstyle.beatecg.activity.HeartHistoryShareActivity;
import com.jstyle.beatecg.activity.ReportActivity;
import com.jstyle.beatecg.adapter.EcgReportAdapter;
import com.jstyle.beatecg.util.ScreenUtils;
import com.jstyle.beatecg.util.SpacesItemDecoration;
import com.jstyle.beatecg.view.RadioView;

import java.util.Arrays;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link HrvReportFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HrvReportFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static String TYPEs = "0";

    Unbinder unbinder;
    @BindView(R.id.radio)
    RadioView radio;
    @BindView(R.id.bt_finish)
    Button btFinish;
    @BindView(R.id.button_restart)
    Button buttonRestart;
    @BindView(R.id.recycleView_ecgReport)
    RecyclerView recycleViewEcgReport;
    @BindArray(R.array.ecg_report_arrays)
    String[]ecgArrays;
    @BindArray(R.array.ecg_report_arrays2)
    String[]ecgArrays2;
    // TODO: Rename and change types of parameters
    private int []mParam1;
    @BindArray(R.array.radio_arrays)
    String[]arrayRadio;
    private String mParam2;
    private static String filepath="filepath";

    private OnFragmentInteractionListener mListener;

    public HrvReportFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *

     * @return A new instance of fragment HrvReportFragment.
     */
    // TODO: Rename and change types and number of parameters
    float[]dataReport;
    public static HrvReportFragment newInstance(String TYPE,int[] report,float[]dataReport,String filepaths) {
        HrvReportFragment fragment = new HrvReportFragment();
        Bundle bundle = new Bundle();
        bundle.putIntArray(ARG_PARAM1, report);
        bundle.putString(TYPEs, TYPE);
        bundle.putFloatArray(ARG_PARAM2, dataReport);
        bundle.putString(filepath, filepaths);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getIntArray(ARG_PARAM1);
            dataReport=getArguments().getFloatArray(ARG_PARAM2);
            TYPEs=getArguments().getString(TYPEs);
            filepath=getArguments().getString(filepath);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_hrv, container, false);
        unbinder = ButterKnife.bind(this, view);

        if(null!=TYPEs&&TYPEs.equals("0")){
            buttonRestart.setText(R.string.restart);
        }else{
            buttonRestart.setText(R.string.history);
        }
        ButterKnife.bind(getActivity());
        initView();
        return view;
    }

    private void initView() {
        LinearLayoutManager gridLayoutManager=new LinearLayoutManager(getActivity());
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleViewEcgReport.setLayoutManager(gridLayoutManager);
        EcgReportAdapter sportTypeAdapter=new EcgReportAdapter(ecgArrays,ecgArrays2,mParam1);
        recycleViewEcgReport.setAdapter(sportTypeAdapter);
        recycleViewEcgReport.addItemDecoration(new SpacesItemDecoration(ScreenUtils.dip2px(getActivity(), 8)));
        radio.setTitles(Arrays.asList(arrayRadio));
        if(dataReport!=null)
            radio.updateData(dataReport);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
//        if (context instanceof OnFragmentInteractionListener) {
//            mListener = (OnFragmentInteractionListener) context;
//        } else {
//            throw new RuntimeException(context.toString()
//                    + " must implement OnFragmentInteractionListener");
//        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.bt_finish, R.id.button_restart})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_finish:
                getActivity().finish();
                break;
            case R.id.button_restart:
                if(TYPEs.equals("0")){
                    Intent intent = new Intent(getActivity(), EcgWatchActivity.class);
                    intent.putExtra("type",EcgWatchActivity.ReSTART);
                    startActivity(intent);
                    getActivity().finish();
                }else{
                    Intent intenta = new Intent(getActivity(), HeartHistoryShareActivity.class);
                    intenta.putExtra("filePath",filepath);
                    startActivity(intenta);
                }

                break;
        }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
